home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / DRAW.PAK / DRAW.CPP next >
C/C++ Source or Header  |  1997-05-06  |  6KB  |  249 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1993, 1995 by Borland International, All Rights Reserved
  4. //----------------------------------------------------------------------------
  5. #include <owl/pch.h>
  6. #include <owl/applicat.h>
  7. #include <owl/framewin.h>
  8. #include <owl/dc.h>
  9. #include <owl/opensave.h>
  10. #include <owl/inputdia.h>
  11. #include <services/cstring.h>
  12. #include <services/dir.h>
  13. #include "draw.rh"
  14.  
  15. class TDrawWindow : public TFrameWindow {
  16.   public:
  17.     TDrawWindow(TWindow* parent, const char far* title);
  18.  
  19.     void CleanupWindow();
  20.  
  21.   protected:
  22.     void EvLButtonDown(uint modKeys, TPoint& point);
  23.     void EvLButtonUp(uint modKeys, TPoint& point);
  24.     void EvMouseMove(uint modKeys, TPoint& point);
  25.     void EvRButtonDown(uint modKeys, TPoint& point);
  26.     void CmRecord();
  27.     void CmStop();
  28.     void CmPlay();
  29.  
  30.   private:
  31.     TClientDC*    HoldDC;
  32.     TMetaFileDC*  MetaFileDC;
  33.     bool          Recording;
  34.     bool          ButtonDown;
  35.  
  36.     TOpenSaveDialog::TData  FileData;
  37.  
  38.     bool CloseMetaFile();
  39.     void DisplayMessage(const string& msg);
  40.  
  41.   DECLARE_RESPONSE_TABLE(TDrawWindow);
  42. };
  43.  
  44. DEFINE_RESPONSE_TABLE1(TDrawWindow, TFrameWindow)
  45.   EV_WM_LBUTTONDOWN,
  46.   EV_WM_LBUTTONUP,
  47.   EV_WM_MOUSEMOVE,
  48.   EV_WM_RBUTTONDOWN,
  49.   EV_COMMAND(CM_RECORD, CmRecord),
  50.   EV_COMMAND(CM_STOP, CmStop),
  51.   EV_COMMAND(CM_PLAY, CmPlay),
  52. END_RESPONSE_TABLE;
  53.  
  54. //
  55. //
  56. //
  57. TDrawWindow::TDrawWindow(TWindow* parent, const char far* title)
  58. :
  59.   TFrameWindow(parent, title)
  60. {
  61.   ButtonDown = false;
  62.   HoldDC = 0;
  63.   MetaFileDC = 0;
  64.   Recording = false;
  65.   AssignMenu("DRAW_MENU");
  66.  
  67.   //
  68.   // Initialize file open data.
  69.   //
  70.   FileData.Flags = OFN_FILEMUSTEXIST|OFN_HIDEREADONLY;
  71.   FileData.SetFilter("MetaFiles (*.WMF)|*.wmf|");
  72. }
  73.  
  74. //
  75. // CleanupWindow(). Close and destroy meta file (disk file remains);
  76. //
  77. void
  78. TDrawWindow::CleanupWindow()
  79. {
  80.   CmStop();
  81. }
  82.  
  83. //
  84. // Define a TDrawWindow's response to an incoming "left-button-down"
  85. // message.  In response, TDrawWindows prepare to draw a line,
  86. // setting the current position in client area, retrieving a display
  87. // context from MS-Windows, and capturing mouse input.  Write drawing to
  88. // metafile if recording.
  89. //
  90. void
  91. TDrawWindow::EvLButtonDown(uint, TPoint& point)
  92. {
  93.   if (!ButtonDown) {
  94.     ButtonDown = true;
  95.     SetCapture();    // Direct all subsequent mouse input to this window
  96.     HoldDC = new TClientDC(*this);
  97.     HoldDC->MoveTo(point);
  98.     if (Recording)
  99.       MetaFileDC->MoveTo(point);
  100.   }
  101. }
  102.  
  103. //
  104. // Define a TDrawWindow's response to an incoming "mouse-move"
  105. // message.  In response, TDrawWindows draw a line using the new
  106. // position of the mouse. Write drawing to metafile if recording
  107. //
  108. void
  109. TDrawWindow::EvMouseMove(uint /*modKeys*/, TPoint& point)
  110. {
  111.   if (ButtonDown) {
  112.     HoldDC->LineTo(point);   // draw the line
  113.     if (Recording)
  114.       MetaFileDC->LineTo(point);
  115.   }
  116. }
  117.  
  118. //
  119. // Define a TDrawWindow's response to an incoming "left-button-up"
  120. // message.  In response, TDrawWindows "cleanup" required after a
  121. // line is drawn, releasing the display context to MS-Windows, and
  122. // releasing mouse input.
  123. //
  124. void
  125. TDrawWindow::EvLButtonUp(uint, TPoint&)
  126. {
  127.   if (ButtonDown) {
  128.     ReleaseCapture();
  129.     delete HoldDC;
  130.     HoldDC = 0;
  131.     ButtonDown = false;
  132.   }
  133. }
  134.  
  135. //
  136. // Define a TDrawWindow's response to an incoming
  137. // "right-button-down" message.  In response, TDrawWindows "clear"
  138. // their client area.
  139. // Invalidate the entire window--Windows will send WM_PAINT message to the
  140. // window.  If recording close metafile and display message.
  141. //
  142. void
  143. TDrawWindow::EvRButtonDown(uint, TPoint&)
  144. {
  145.   Invalidate(true);
  146.   CmStop();
  147. }
  148.  
  149. //
  150. // Ask user for a filename and create metafile.  Set flag indicating that
  151. // we are recording metafile.  If already recording then close it and display
  152. // message.
  153. //
  154. void
  155. TDrawWindow::CmRecord()
  156. {
  157.   *FileData.FileName = 0;
  158.   if (TFileSaveDialog(this, FileData, 0, "Record MetaFile").Execute() == IDOK) {
  159.     CmStop();
  160.     MetaFileDC = new TMetaFileDC(FileData.FileName);
  161.     Recording = true;
  162.     Invalidate(true);                       // clear the client area.
  163.   }
  164. }
  165.  
  166. //
  167. // Stop any current MetaFile recording.
  168. //
  169. void
  170. TDrawWindow::CmStop()
  171. {
  172.   if (CloseMetaFile())
  173.     DisplayMessage("Current MetaFile recording complete.");
  174. }
  175.  
  176. //
  177. // Ask user for a file to play.  Assumes file is a metafile (PlayOnto will
  178. // fail if it isn't).  If already recording then close it and display message.
  179. // Before playing metafile clear client area.
  180. //
  181. void
  182. TDrawWindow::CmPlay()
  183. {
  184.   *FileData.FileName = 0;
  185.   if (TFileOpenDialog(this, FileData, 0, "Open MetaFile").Execute() == IDOK) {
  186.     CmStop();
  187.     Invalidate(true);                       // clear the client area.
  188.     UpdateWindow();
  189.     TMetaFilePict mf(FileData.FileName);
  190.     HoldDC = new TClientDC(*this);
  191.     mf.PlayOnto(*HoldDC, GetClientRect().Size());
  192.     delete HoldDC;
  193.     HoldDC = 0;
  194.   }
  195. }
  196.  
  197. //
  198. // Close and destroy metafile, disk file remains.  Return true if we were
  199. // recording, else false.
  200. //
  201. bool
  202. TDrawWindow::CloseMetaFile()
  203. {
  204.   if (Recording) {
  205.     delete MetaFileDC;
  206.     Recording = false;
  207.     return true;
  208.   }
  209.   return false;
  210. }
  211.  
  212. //
  213. // Display a message to user.
  214. //
  215. void
  216. TDrawWindow::DisplayMessage(const string& msg)
  217. {
  218.   MessageBox(msg.c_str(), GetApplication()->GetName(), MB_OK);
  219. }
  220.  
  221. //----------------------------------------------------------------------------
  222.  
  223. //
  224. //
  225. //
  226. class TDrawApp : public TApplication {
  227.   public:
  228.     TDrawApp() : TApplication("Draw") {}
  229.     void InitMainWindow();
  230. };
  231.  
  232. //
  233. //
  234. //
  235. void
  236. TDrawApp::InitMainWindow()
  237. {
  238.   MainWindow = new TDrawWindow(0, "Draw Away!");
  239. }
  240.  
  241. //
  242. //
  243. //
  244. int
  245. OwlMain(int /*argc*/, char* /*argv*/ [])
  246. {
  247.   return TDrawApp().Run();
  248. }
  249.